package com.newbee.activity;

import java.io.File;

import com.hhu.newbee.R;
import com.newbee.activity.tab.PasswordFixActivity;
import com.newbee.sqlite.DBHelper;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class UserConfigActivity extends Activity implements OnClickListener
{
	private Builder builder;

	private DBHelper dbHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_config);
		findViewById(R.id.fix_password).setOnClickListener(this);
		findViewById(R.id.delete_all_file).setOnClickListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		dbHelper = new DBHelper(this);
		builder = new Builder(this);
		builder.setTitle("警告");
		builder.setMessage("你确定要清空所有的录音?");
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener()
				{

					@Override
					public void onClick(DialogInterface arg0, int arg1)
					{
						deleteALLFile();
						dbHelper.deleteAllLuYin();
						Toast.makeText(UserConfigActivity.this, "删除成功！",
								Toast.LENGTH_LONG).show();

					}

				}).setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener()
				{

					@Override
					public void onClick(DialogInterface arg0, int arg1)
					{
						// do nothing!
					}

				});

	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.fix_password:
			Intent intent = new Intent(this, PasswordFixActivity.class);
			this.startActivity(intent);
			break;
		case R.id.delete_all_file:
			builder.create().show();
			break;
		case R.id.back:
		{
			this.finish();
			break;
		}

		default:
			break;
		}

	}

	private void deleteALLFile()
	{

		File file = Environment.getExternalStorageDirectory();
		if (file.isDirectory())
		{

			File[] fl = file.listFiles();
			for (File f : fl)
			{
				if (f.getName().endsWith("vtf") || f.getName().endsWith("spx"))
				{
					f.delete();
				}
			}
		}

	}
}
